package com.loveyukari.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loveyukari.common.Pager;
import com.loveyukari.model.Music;
import com.loveyukari.service.MusicService;
import com.loveyukari.support.JsonResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
*
*yangshaohua
*2017年3月3日下午1:47:19
**/
@Controller
@RequestMapping("/admin/music")
public class MusicController {
	
	@Autowired
	private MusicService musicService;
	
	@RequestMapping("/list")
	public String list(Model model,String type,Pager pager){
		return "music/list";
	}
	
	@RequestMapping("/lyrics")
	public String list(Model model,int id){
		Music music = musicService.getMusic(id);
		String lyrics = music.getContent();
		lyrics = lyrics.replace("\r\n", "</br>");
		model.addAttribute("lyrics", lyrics);
		return "music/lyrics";
	}
	
	@RequestMapping("/search") 
	@ResponseBody
	public JsonResult<Object> search(String type,String query,String orderType,boolean desc){
		List<Music> list = musicService.searchMusicList(type, query, orderType, desc);
		return new JsonResult<Object>(0,"",list);
	}
	
	//添加歌曲
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String add(){
		return "music/add";
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Music music){
		musicService.addMusic(music);
		return "redirect:/admin/music/list";
	}
	
	//编辑歌曲
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public String edit(Model model,int id){
		Music music = musicService.getMusic(id);
		model.addAttribute("music", music);
		return "music/edit";
	}
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public String edit(Music music){
		musicService.editMusic(music);
		return "redirect:/admin/music/list";
	}
	
	
	
	//随机歌单
	@RequestMapping("/random/single")
	public String randomSingle(Model model){
		List<String> initTxts = new ArrayList<String>();
		initTxts.add("大王抽我~啊~");
		initTxts.add("说不定有武娘呢");
		initTxts.add("反正没有救爷爷");
		initTxts.add("就算抽中了也不一定要唱嘛");
		initTxts.add("抽奖玩腻了下次可以刮刮卡");
		initTxts.add("干杯干杯干杯干杯干杯干杯");
		initTxts.add("地球是缘的，小缘是我的");
		JSONArray names = musicService.getAllMusicName();
		int index = new Random().nextInt(initTxts.size());
		model.addAttribute("initTxt", initTxts.get(index));
		model.addAttribute("names", names.toString());
		return "music/random_single";
	}
	
	@RequestMapping("/random/index")
	public String randomIndex(){
		return "music/random_index";
	}
	
	@RequestMapping("/random/music")
	@ResponseBody
	public JsonResult<Object> randomMusic(int size){
		return null;
	}
	
	
	//直播歌单
	@RequestMapping("/sheet/list")
	public String sheetList(Model model){
		List<JSONObject> list = musicService.getLiveSongCountList();
		model.addAttribute("list", list);
		return "music/sheet_list";
	}
	@RequestMapping("/sheetDetail/{date}")
	public String sheetDetail(Model model,@PathVariable String date){
		List<Music> list = musicService.getLiveMusic(date);
		model.addAttribute("date", date);
		model.addAttribute("count", list.size());
		try {
			model.addAttribute("week", new SimpleDateFormat("EEEE").format(new SimpleDateFormat("yyyy-MM-dd").parse(date)));
		} catch (ParseException e) {
		}
		model.addAttribute("list",list);
		return "music/sheet_detail";
	}
	
	//添加歌单
	@RequestMapping("/sheetAddSearch")
	@ResponseBody
	public JsonResult<Object> sheetAddSearch(String query,int size){
		List<Music> list = musicService.searchMusicList(query, size);
		return new JsonResult<Object>(0,"",list);
	}
	
	@RequestMapping(value="/sheet/add",method=RequestMethod.GET)
	public String sheetAdd(){
		return "music/sheet_add";
	}
	@RequestMapping(value="/sheet/add",method=RequestMethod.POST)
	public String sheetAdd(int[] ids,int[] ranks,String date){
		if (ArrayUtils.isEmpty(ids) || ArrayUtils.isEmpty(ranks) || StringUtils.isEmpty(date)){
			return "redirect:/admin/music/sheet/add";
		}
		musicService.addMusicSheet(ids, ranks, date);
		return "redirect:/admin/music/sheet/list";
	}
	
}
